#!/bin/bash

lang='ru'

(
    echo '<?xml version="1.0" encoding="utf-8"?>'
    echo '<channels>'
    cat playlist.m3u |
        while read s; do
            if [ $(echo $s | grep -c 'EXTINF') -gt 0 ]; then
                n=$(echo $s | sed 's/#EXTINF:[0-9]*,//; s/ .*//');
                title=$(echo $s | sed 's/#EXTINF:[0-9]*,[0-9]* //');
            else if [ $(echo $s | grep -c 'udp://@') -gt 0 ]; then
                url=$(echo $s | sed 's/udp:\/\/@//');
                echo '    <channel id="'$n'">'
                echo '        <name lang="'$lang'">'$title'</name>'
                echo '        <link>'$url'</link>'
                echo '    </channel>'
            fi fi
        done
    echo '</channels>'
) >channels.xml
